<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ItemModel;
use cmf\controller\AdminBaseController;

class ItemController extends AdminBaseController {

	public function gpsprice() {
		$ItemModel = new ItemModel();
		$request = input('request.');
		$where = array('type' => 1);
		if (!empty($request['keyword'])) {
			$keyword = $request['keyword'];
			$where['name'] = ['like', "%$keyword%"];
		}
		$list = $ItemModel->where($where)->paginate(10);
		$page = $list->render();
		$this->assign('list', $list);
		$this->assign('page', $page);

		return $this->fetch("gps_index");
	}

	public function textprice() {
		$ItemModel = new ItemModel();
		$request = input('request.');
		$where = array('type' => 2);
		if (!empty($request['keyword'])) {
			$keyword = $request['keyword'];
			$where['name'] = ['like', "%$keyword%"];
		}
		$list = $ItemModel->where($where)->paginate(10);
		$page = $list->render();
		$this->assign('list', $list);
		$this->assign('page', $page);

		return $this->fetch("text_index");
	}

	/**
	 * 幻灯片列表
	 * @adminMenu(
	 *     'name'   => '幻灯片管理',
	 *     'parent' => 'admin/Setting/default',
	 *     'display'=> true,
	 *     'hasView'=> true,
	 *     'order'  => 40,
	 *     'icon'   => '',
	 *     'remark' => '幻灯片管理',
	 *     'param'  => ''
	 * )
	 */
	public function index() {
		$OrderModel = new OrderModel();
		$request = input('request.');
		$where = array();
		if (!empty($request['keyword'])) {
			$keyword = $request['keyword'];
			$where['uid'] = ['like', "%$keyword%"];
		}
		$list = $OrderModel->where($where)->paginate(10);
		$page = $list->render();
		$this->assign('list', $list);
		$this->assign('page', $page);

		return $this->fetch();
	}

	public function add() {
		return $this->fetch();
	}

	public function edititem() {
		$data = $this->request->param();
		$id = $data['id'];
		$model = new ItemModel();
		$item = $model->find($id);
		$this->assign('item', $item);
		if ($item['type'] == 1) {
			$page = "edit_gps";
		} else {
			$page = "edit_text";
		}
		return $this->fetch($page);
	}

	public function postitem() {
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$model = new ItemModel();
			if (isset($data['post']['id']) && $data['post']['id']) {
			    $id = $data['post']['id'];
			    unset($data['post']['id']);
			    $model->where(['id' => $id])->update($data['post']);
			} else {
				$model->insert($data['post']);
			}
			if ($data['post']['type'] == 1) {
				$page = "admin/item/gpsprice";
			} else {
				$page = "admin/item/textprice";
			}
			$this->success('操作成功!', url($page));
		}
	}

	public function editdevice() {
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$model = new DeviceModel();
			$model->where(['id' => $data['post']['id']])->update($data['post']);
			$this->success('修改成功!', url('admin/device/index'));
		}
	}

	public function deleteitem() {
		$id = $data = $this->request->param("id");
		$model = new ItemModel();
		$model->where(['id' => $id])->delete();
		$this->success('删除成功!', url('admin/item/gpsprice'));
	}

	public function adddevice() {

		if ($this->request->isPost()) {
			$data = $this->request->param();
			$model = new DeviceModel();
			$id = $model->insert($data['post']);
//            $post   = $data['post'];
			//            $result = $this->validate($post, 'AdminArticle');
			//            if ($result !== true) {
			//                $this->error($result);
			//            }

//            $portalPostModel = new PortalPostModel();
			//
			//            if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
			//                $data['post']['more']['photos'] = [];
			//                foreach ($data['photo_urls'] as $key => $url) {
			//                    $photoUrl = cmf_asset_relative_url($url);
			//                    array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
			//                }
			//            }
			//
			//            if (!empty($data['file_names']) && !empty($data['file_urls'])) {
			//                $data['post']['more']['files'] = [];
			//                foreach ($data['file_urls'] as $key => $url) {
			//                    $fileUrl = cmf_asset_relative_url($url);
			//                    array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
			//                }
			//            }
			//
			//            $portalPostModel->adminAddArticle($data['post'], $data['post']['categories']);
			//
			//            $data['post']['id'] = $portalPostModel->id;
			//            $hookParam          = [
			//                'is_add'  => true,
			//                'article' => $data['post']
			//            ];
			//            hook('portal_admin_after_save_article', $hookParam);

			$this->success('添加成功!', url('admin/device/index'));
		}
	}

	//添加gps价格
	public function add_gps() {
		return $this->fetch();
	}

	//添加gps价格
	public function add_text() {
		return $this->fetch();
	}

}